﻿using EntityFrameworks.Web.Core.Models;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace EntityFrameworks.Web.Core
{
    /// <summary>
    /// 树形结构管理器
    /// </summary>
    public class TreeManager
    {
        List<Tree> _subList;
        public TreeManager(List<Tree> subList)
        {
            this._subList = subList;
        }

        #region 树型结构相关
        /// <summary>
        /// 递归找老祖宗
        /// </summary>
        /// <param name="father"></param>
        public void GetFather(Tree father)
        {
            if (father != null)
            {
                father.Parent = _subList.FirstOrDefault(i => i.ID == father.ParentID);
                GetFather(father.Parent);
            }
        }

        /// <summary>
        /// 弟妹找子孙
        /// </summary>
        /// <param name="father">父对象</param>
        void GetSons(Tree father)
        {
            if (father != null)
            {
                father.Sons = _subList.Where(item =>
                    item.ParentID.Equals(father.ID)).ToList();
                father.Sons.ForEach(item =>
                {
                    item.Parent = father;
                    GetSons(item);
                });
            }
        }

        /// <summary>
        /// 递归得到它的所有祖宗以selectlist的形式进行拼接
        /// </summary>
        /// <param name="son"></param>
        /// <param name="sbr"></param>
        public void GetSelectList(Tree son, StringBuilder sbr)
        {
            StringBuilder inSbr = new StringBuilder();
            if (son != null)
            {
                if (!son.ParentID.HasValue || son.ParentID.Value == 0)
                    inSbr.Append("<select name='Parent' onchange = 'treeOnSelect(this)'>");
                else
                    inSbr.Append("<select name='Sub'>");
                GetTreeByLevel(son.Level).ToList().ForEach(i =>
                {
                    if (i.ID == son.ID)
                        inSbr.Append("<option value='" + i.ID + "' selected='true'>" + i.Name + "</option>");
                    else
                        inSbr.Append("<option value='" + i.ID + "'>" + i.Name + "</option>");
                });

                inSbr.Append("</select>");
                sbr.Insert(0, inSbr);
                GetSelectList(son.Parent, sbr);
            }
        }

        /// <summary>
        /// 得到指定深度的列表
        /// </summary>
        /// <param name="level"></param>
        /// <returns></returns>
        public List<Tree> GetTreeByLevel(int level)
        {
            var linq = from data1 in _subList
                       join data2 in _subList on data1.ParentID equals data2.ID into list
                       select new Tree
                       {
                           ID = data1.ID,
                           Level = data1.Level,
                           Name = data1.Name,
                           Parent = list.FirstOrDefault(),
                           ParentID = data1.ParentID,
                       };
            return linq.Where(i => i.Level.Equals(level)).ToList();
        }

        #endregion
    }
}
